public class Peopletest42 {
    public static void main(String[] args) {
        //1.创建一个数组来储存学生对象
        People[] arr = new People[3];
//2.创建学生对象并添加到数组中
        People stu1 = new People(1, "zhang", 21);
        People stu2 = new People(2, "wang", 23);
        People stu3 = new People(3, "zhao", 22);
//3.把学生添加到数组中
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
 //要求5:查询数组中id为"2"的学生,如果存在,则将他的年龄+1岁
    int index = getIndex(arr,2);
    if(index >= 0){
          //存在,将年龄+1
        People p =arr[index];
        int newAge = p.getAge()+1;
        p.setAge(newAge);
        getArr(arr);
    }else{
        System.out.println("不存在该学号");
    }
    }
    public static void getArr(People[] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                System.out.println(arr[i].getId()+","+arr[i].getName()+","+arr[i].getAge());
            }
        }
    }
    public static int getIndex(People[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                int pid = arr[i].getId();
                if(pid == id){
                    return i;
                }
            }
        }
        return -1;
    }
}
